class Printer
{
public:
    // 1 2 3 4 5
    // 1 2 3 4 5
    // 1 2 3 4 5

    // [x][y]         ... [x]][m - y - 1]
    //  ...              ...
    // [n - x - 1][y] ... [n - x - 1][m - y - 1]

    vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m)
    {
        vector<int> ret;
        int x = 0;
        int y = 0;
        while (2 * x + 1 <= n || 2 * y + 1 <= m)
        {
            if (2 * x + 1 < n && 2 * y + 1 < m)
            {
                for (int i = y; i < m - y - 1; ++i)
                {
                    ret.push_back(mat[x][i]);
                }
                for (int i = x; i < n - x - 1; ++i)
                {
                    ret.push_back(mat[i][m - y - 1]);
                }
                for (int i = m - y - 1; i > y; --i)
                {
                    ret.push_back(mat[n - x - 1][i]);
                }
                for (int i = n - x - 1; i > x; --i)
                {
                    ret.push_back(mat[i][y]);
                }
                ++x;
                ++y;
                continue;
            }
            else if (2 * x + 1 == n)
            {
                for (int i = y; i <= m - y - 1; ++i)
                {
                    ret.push_back(mat[x][i]);
                }
                break;
            }
            else if (2 * y + 1 == m)
            {
                for (int i = x; i <= n - x - 1; ++i)
                {
                    ret.push_back(mat[i][m - y - 1]);
                }
                break;
            }
            break;
        }
        return ret;
    }
};